//
//  RankingTableViewController.m
//  IntegralDog
//
//  Created by wmm on 16/1/27.
//  Copyright © 2016年 hanen. All rights reserved.
//

#import "RankingTableViewController.h"
#import "RankingTableViewCell.h"
#import "Macro.h"
#import "PersonalNetTool.h"
#import "personalModel.h"
#import "UIImageView+WebCache.h"
#import "DateTool.h"

@interface RankingTableViewController ()
{
    NSMutableArray *rankingArr;
    UITableView *excTableView;
}

@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UISegmentedControl *segment;
@property (nonatomic, strong) UIImageView *dateImgView;

@end

@implementation RankingTableViewController

- (void)viewDidAppear:(BOOL)animated
{
    [self getRankingWithType:1];
    
}
//网络请求
- (void)getRankingWithType:(int)Type
{
    [PersonalNetTool getRankingWithType:Type complationBlock:^(NSDictionary *dic) {
        [rankingArr removeAllObjects];
        for (NSArray *arr in [dic objectForKey:@"list"]) {
            [rankingArr addObject:arr];
        }
        [excTableView reloadData];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"排行榜";
    self.navigationController.navigationBarHidden = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    rankingArr = [[NSMutableArray alloc] init];
    //    self.navigationItem.backBarButtonItem.title = @"";
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 12, 22)];
    [btn addTarget:self action:@selector(backToView:) forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:[UIImage imageNamed:@"Btn_Nourmal_Jiantou"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"Btn_Push_Jiantou"] forState:UIControlStateSelected];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = barItem;
    
    NSArray *arr = [[NSArray alloc]initWithObjects:@"日榜",@"周榜",@"月榜",@"总榜", nil];
    _segment = [[UISegmentedControl alloc]initWithItems:arr];
    //在没有设置[segment setApportionsSegmentWidthsByContent:YES]时，每个的宽度按segment的宽度平分
    _segment.frame = CGRectMake(10, 74, kScreenWidth-20, 30);
    _segment.selectedSegmentIndex = 0;
    _segment.tintColor = mainOrangeColor;
    [_segment addTarget:self action:@selector(changeRankView:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_segment];
    
    //    UIView *dateView = [[UIView alloc]initWithFrame:CGRectMake(0.0f, 130.0f, kScreenWidth, 40)];
    //    dateView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"rank_bgPaihangbeijing"]];
    //
    //    UIImageView *dateImgView = [[UIImageView alloc]initWithFrame:CGRectMake(80.0f, 140.0f, kScreenWidth, 30)];
    //    dateImgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"rank_bgPaihangbeijing"]];
    //
    //    [self.view addSubview:dateView];
    //
    //    [self.view addSubview:dateView];
    
    
    UIImageView *dateView = [[UIImageView alloc]init];
    dateView.frame = CGRectMake(0, _segment.maxY + 10, kScreenWidth, 40);
    dateView.image = [UIImage imageNamed:@"rank_bgPaihangbeijing"];
    [self.view addSubview:dateView];
    
    _dateLabel =[[UILabel alloc]init];
    _dateLabel.frame = CGRectMake(10, 10, kScreenWidth, 20);
    _dateLabel.textAlignment = NSTextAlignmentCenter;
    _dateLabel.lineBreakMode = NSLineBreakByWordWrapping;
    
    
    NSDate *  senddate=[NSDate date];
    
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    
    [dateformatter setDateFormat:@"YYYY.MM.dd"];
    
    NSString *  locationString=[dateformatter stringFromDate:senddate];
    _dateLabel.text =[NSString stringWithFormat:@"%@  0:00-24:00",locationString];
    _dateLabel.textColor = [UIColor whiteColor];
    _dateLabel.font = [UIFont boldSystemFontOfSize:15];
    [dateView addSubview:_dateLabel];
    
    CGSize size = [_dateLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]} context:nil].size;
    
    _dateImgView = [[UIImageView alloc]init];
    _dateImgView.frame = CGRectMake((kScreenWidth-size.width)/2 -20, 10, 20, 20);
    _dateImgView.image = [UIImage imageNamed:@"rank_Shijianjilu"];
    [dateView addSubview:_dateImgView];
    
    
    
    excTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, dateView.maxY, kScreenWidth, kScreenHeight - dateView.maxY) style:UITableViewStylePlain];
    excTableView.delegate=self;
    excTableView.dataSource=self;
    excTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.view.userInteractionEnabled = YES;
    
    [self.view addSubview:excTableView];
    
}
- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}

//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        self.hidesBottomBarWhenPushed=YES;
//    }
//    return self;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return rankingArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"RankingTableViewCell";
    RankingTableViewCell *cell = (RankingTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[RankingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.rankNumLab.text = [NSString stringWithFormat: @"%ld", (long)(indexPath.row+1)];
    if (indexPath.row == 0) {
        cell.rankNumLab.text = @"NO.1";
    }
    if (indexPath.row == 1) {
        cell.rankNumLab.text = @"NO.2";
    }
    if (indexPath.row == 2) {
        cell.rankNumLab.text = @"NO.3";
    }
    
    if (rankingArr.count != 0) {
        personalModel *model = [[personalModel alloc] initWithDictionary:[rankingArr objectAtIndex:indexPath.row]];
        cell.rankNameLab.text = [NSString stringWithFormat: @"%@",model.nickname];
        cell.rankIntegralLab.text = [NSString stringWithFormat: @"%@",model.reward];
        [cell.rankHeaderView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMAGEURL,model.logo]] placeholderImage:[UIImage imageNamed:@"uc_Touxiang"]];
        
        NSLog(@"***********888888     %@",rankingArr);
        
        CGSize size = [cell.rankNameLab.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]} context:nil].size;
        CGFloat imageX;
        if (size.width > 70) {
            imageX = cell.rankNameLab.maxX;
        }else{
            imageX = cell.rankNameLab.x + size.width;
        }
        cell.rankNumImgView.frame = CGRectMake(imageX + 8 , 23, 15, 15);
        if (indexPath.row == 0) {
            cell.rankNumImgView.image = [UIImage imageNamed:@"rank_Bangshou"];
        }
        if (indexPath.row == 1) {
            cell.rankNumImgView.image = [UIImage imageNamed:@"rank_Dier"];
        }
        if (indexPath.row == 2) {
            cell.rankNumImgView.image = [UIImage imageNamed:@"rank_Disan"];
        }
    }
    
    return cell;
}

-(void)changeRankView:(UISegmentedControl *)control{
    if (_segment.selectedSegmentIndex == 0) {
        [self getRankingWithType:1];
        NSLog(@"111");
        NSDate *  senddate=[NSDate date];
        
        NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
        
        [dateformatter setDateFormat:@"YYYY.MM.dd"];
        
        NSString *  locationString=[dateformatter stringFromDate:senddate];
        _dateLabel.text =[NSString stringWithFormat:@"%@  0:00-24:00",locationString];
        CGSize size = [_dateLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]} context:nil].size;
        _dateImgView.frame = CGRectMake((kScreenWidth-size.width)/2 -20, 10, 20, 20);
        
    }else if(_segment.selectedSegmentIndex == 1){
        [self getRankingWithType:2];
        
        _dateLabel.text = [DateTool getWeekTime];
        
        CGSize size = [_dateLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]} context:nil].size;
        _dateImgView.frame = CGRectMake((kScreenWidth-size.width)/2 -20, 10, 20, 20);
        
        
    }else if(_segment.selectedSegmentIndex == 2){
        [self getRankingWithType:3];
        
        NSDate *  senddate=[NSDate date];
        
        NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
        
        [dateformatter setDateFormat:@"YYYY.MM"];
        
        NSString *  locationString=[dateformatter stringFromDate:senddate];
        _dateLabel.text =locationString;
        
        CGSize size = [_dateLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]} context:nil].size;
        _dateImgView.frame = CGRectMake((kScreenWidth-size.width)/2 -20, 10, 20, 20);
        
    }else if(_segment.selectedSegmentIndex == 3){
        [self getRankingWithType:0];
        
        _dateLabel.text =@"2016.2.29--";
        
        CGSize size = [_dateLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]} context:nil].size;
        _dateImgView.frame = CGRectMake((kScreenWidth-size.width)/2 -20, 10, 20, 20);
    }
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}


- (void)backToView:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
